package cn.kimoon.dau_android.Dialog;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import cn.kimoon.dau_android.R;

import java.lang.reflect.Field;


public class LoadingDialog extends DialogFragment {

    private int DEFAULT_DISMISS_TIME = 1500;
    private TextView title;
    private ImageView circle;
    private int defaultDismissTime;
    private LoadingState state = LoadingState.STATE_START_LOADING;
    private String titleText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.loading_dialog_style);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(false);
        defaultDismissTime = DEFAULT_DISMISS_TIME;

        View view = inflater.inflate(R.layout.dialog_common_loading, container,
                false);
        initView(view);

        switch (state) {
            case STATE_START_LOADING:
                if (TextUtils.isEmpty(titleText)) {
                    titleText = getActivity().getResources().getString(R.string.dialog_loading);
                }
                setStateInfo(R.mipmap.im_loading, titleText);
                startAnimation(circle);
                break;
            case STATE_SUCCEED:
                setStateInfo(R.mipmap.im_loading_succeed, titleText);
                disMissDialogDelay();
                break;
            case STATE_FAIL:
                setStateInfo(R.mipmap.im_loading_fail, titleText);
                disMissDialogDelay();
                break;
            case STATE_WARNING:
                setStateInfo(R.mipmap.im_loading_warning, titleText);
                disMissDialogDelay();
                break;
            default:
                break;
        }
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //设置标题
    public void setTitle(String str) {
        titleText = str;
    }

    //设置消失时间
    public void setDefaultDismissTime(int time) {
        this.defaultDismissTime = time;
    }

    private void initView(View view) {
        title = (TextView) view.findViewById(R.id.title);
        circle = (ImageView) view.findViewById(R.id.circle);

        if (!TextUtils.isEmpty(titleText)) {
            title.setText(titleText);
        }

    }

    //开始加载动画
    private void startAnimation(View view) {
        if (view == null) {
            return;
        }
        view.clearAnimation();

        RotateAnimation animation = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(1200);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE);

        view.startAnimation(animation);
    }

    //停止加载动画
    private void clearAnimation() {
        if (circle == null) {
            return;
        }
        circle.clearAnimation();
    }

    private void disMissDialogDelay() {
        clearAnimation();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissAllowingStateLoss();
            }
        }, defaultDismissTime);
    }

    private void setStateInfo(int resId, String msg) {
        if (circle != null) {
            circle.setImageResource(resId);
        }
        if (title != null) {
            title.setText(msg);
        }
    }

    //设置状态
    public void setState(LoadingState state, String titleText) {
        switch (state) {
            case STATE_START_LOADING:
                break;
            case STATE_SUCCEED:
                setStateInfo(R.mipmap.im_loading_succeed, titleText);
                disMissDialogDelay();
                break;
            case STATE_FAIL:
                setStateInfo(R.mipmap.im_loading_fail, titleText);
                disMissDialogDelay();
                break;
            case STATE_WARNING:
                setStateInfo(R.mipmap.im_loading_warning, titleText);
                disMissDialogDelay();
                break;
            default:
                break;
        }
    }

    //初始化状态
    public void initState(LoadingState state, String titleText, int duration) {
        this.state = state;
        this.titleText = titleText;
        this.defaultDismissTime = duration;
    }

    public enum LoadingState {
        STATE_START_LOADING,
        STATE_SUCCEED,
        STATE_FAIL,
        STATE_WARNING
    }

}
